package com.sphd.emanu.fragment.daydetail;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.sphd.emanu.R;
import com.sphd.emanu.activity.itemdetail.ItemDetailActivity;
import com.sphd.emanu.adapter.DayDetailAdapter;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.fragment.time.TimeFragment;
import com.sphd.emanu.interfaces.DayDetailFragmentLetter;
import com.sphd.emanu.mvp.MVPBaseFragment;
import com.sphd.emanu.pojo.bank.AccountDetailsBean;
import com.sphd.emanu.pojo.bank.AccountPeriodListByAccountBean;
import com.sphd.emanu.pojo.bank.DayDetailMessage;
import com.sphd.emanu.pojo.bank.TimeShowMoneyMessage;
import com.sphd.emanu.pojo.login.User;
import com.wzutils.L;
import com.wzutils.TimeUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * todo 天的明细
 *
 * @Author Hu
 * @Time 2017/7/12 -- 15:32.
 * @Email 87683202@qq.com
 */

public class DayDetailFragment extends MVPBaseFragment<DayDetailContract.View, DayDetailPresenter> implements DayDetailContract.View {

    @BindView(R.id.my_recycler)
    RecyclerView myRecycler;
    @BindView(R.id.my_swf)
    SwipeRefreshLayout mySwf;
    Unbinder unbinder;
    Bundle bundle;
    User user;
    int accountId;
    boolean showIncome, showExpend;
    DayDetailAdapter dayDetailAdapter;
    private String datetime;
    DayDetailFragmentLetter detailFragmentLetter;
    Intent intent;

    public void setDetailFragmentLetter(DayDetailFragmentLetter detailFragmentLetter) {
        this.detailFragmentLetter = detailFragmentLetter;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.myswf_and_recycler;
    }

    @Override
    protected void initView() {
        intent = new Intent();
        bundle = getArguments();
        unbinder = ButterKnife.bind(this, rootView);
        showIncome = bundle.getBoolean(Constant.ShowIncome);
        showExpend = bundle.getBoolean(Constant.ShowExpend);
        initEvent();
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        setRefresh(mySwf);
        initAdapter();
        setUserVisibleHint(true);
        detailFragmentLetter.readyedDayFragment();
    }

    private void initEvent() {

        mySwf.setOnRefreshListener(() -> {
            addData();
        });
        myRecycler.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                AccountDetailsBean accountDetailsBean = (AccountDetailsBean) adapter.getItem(position);
                bundle.putSerializable(Constant.ACCOUNT_DETAILS_BEAN, accountDetailsBean);
                intent.putExtras(bundle);
                intent.setClass(context, ItemDetailActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initAdapter() {
        setRecy(myRecycler);
        dayDetailAdapter = new DayDetailAdapter(R.layout.recycler_daydetail_item, new ArrayList<AccountDetailsBean>());
        dayDetailAdapter.setEmptyView(emptyView);
        myRecycler.setAdapter(dayDetailAdapter);
    }

    @Override
    protected void initData() {

    }

    public void swfData(String datetime, int accountId) {
        this.datetime = datetime;
        this.accountId = accountId;
        addData();
    }

    private void addData() {
        showProgressDialog();
        mPresenter.getDetailListByDay(user.getOid(), datetime, accountId != 0 ? String.valueOf(accountId) : null);
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void setBean(DayDetailMessage body) {
        dissProgressDialog();
        if (showIncome && showExpend) {
            dayDetailAdapter.setNewData(body.getAccountDetails());

        } else if (showIncome && !showExpend) {
            dayDetailAdapter.setNewData(removeDebitList(body.getAccountDetails()));

        } else if (!showIncome && showExpend) {
            dayDetailAdapter.setNewData(removeCreditList(body.getAccountDetails()));
        }
    }

    /*移除支出数据只显示收入*/
    public List<AccountDetailsBean> removeDebitList(List<AccountDetailsBean> data) {
        List<AccountDetailsBean> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {

            if (data.get(i).getCredit() > 0) {
                list.add(data.get(i));
            }
        }

        return list;
    }

    /*移除收入数据只显示支出*/
    public List<AccountDetailsBean> removeCreditList(List<AccountDetailsBean> data) {
        List<AccountDetailsBean> list = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getDebit() > 0) {
                list.add(data.get(i));
            }
        }

        return list;
    }
}
